
import { observable, action, toJS } from 'mobx';
import { ListStore } from './listStore'
import { WeaTools } from 'ecCom';
import * as API_LIST from '../apis/inventory';
import { WeaTableNew, WeaForm } from 'comsMobx'
import { message, Modal } from 'antd';
const { TableStore } = WeaTableNew;
import {WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

export default class MyInventoryStore extends ListStore {

    baseParams = {};
    @observable searchParams ={
        tabkey : 0,
        viewcondition: 0
    }
    @observable selectid = '';
    @observable inventoryId = '';
    @observable basicInfo = [];
    @observable planlistForm = new WeaForm();
    @observable subListData = [];
    @observable plantableStore = new TableStore();
    @observable sessionkey_plan = "";
    @observable visible = false;
    @observable inventoryVisible = false;
    //盘点
    @observable inventoryform = new WeaForm();
    @observable inventoryVisible = false;
    @observable inventoryformCondition = [];
    @observable inventoryformCheckStr = {};
    @observable maintitle = '';

    @observable canEditCpt = false;
    @observable importVisible = false;
    @observable hasUpload = false;
    @observable spinning = false;
    @observable msg = "";
    @observable reflesh = 0;
    @observable filelist = [];
    @observable excelfile = '';
    @observable remind;

    constructor() {
        super();
        this.appendBaseParams = this.appendBaseParams.bind(this);
        this.doSearch = this.doSearch.bind(this);
        this.clearStatus = this.clearStatus.bind(this);
        this.initDatas = this.initDatas.bind(this);
    }

    @action
    appendBaseParams(params) {
        this.baseParams = { ...this.baseParams, ...params };
    }

    @action
    initDatas = (params = {}) => {
        API_LIST.getCptInventoryList({ ...this.baseParams, ...params, viewScope: "MyInventoryList", actionType: "baseinfo" }).then((data) => {
            const { groupinfo, pagetitle, conditioninfo } = data;
            // this.title = pagetitle;
            this.condition = conditioninfo;
            //根据高级搜索条件初始化form
            this.form && !this.form.isFormInit && this.form.initFormFields(conditioninfo);
        });
        this.searchParams = { ...this.searchParams, ...params };
    }

    @action
    doSearch = (params = {}) => {
        //获取表单的参数值
        const searchParamsAd = this.form.getFormParams();
        const newParams = { ...this.baseParams, ...this.searchParams, ...searchParamsAd, ...params, viewScope: "MyInventoryList", actionType: "splitpage" };
        this.loading = true;
        API_LIST.getCptInventoryList(newParams).then((data) => {
            this.tableStore = new TableStore();
            this.tableStore.getDatas(data.sessionkey, params.current || 1);
            this.dataKey = data.sessionkey;
            this.loading = false;//最后一次必须是修改观察模式的状态，因为前面dataKey及sharearg未设置观察者模式，减少render次数
        });

    }

    @action 
    changeTab = (key)=>{
        this.searchParams={...this.searchParams,tabkey:key};
        this.doSearch();
        
    }

    clearStatus = () => {
        this.searchParams = { viewcondition: 0,tabkey:0 };
        this.clearFormFields();
    }

    //设置弹出窗可见性
    setVisiable = (visiable, index) => {
        if (index == 1) {
            // this.addform.resetConditionValue();//清除添加表单默认值  
            this.inventoryVisible = visiable
        }
        if (index == 2) {
            // this.addform.resetConditionValue();//清除添加表单默认值  
            this.importVisible = visiable
        }
    }

    setSelectId = (params) => { //得到选择的id    
        //alert(params.selectid)   
        this.selectid = params.selectid;
    }

    @action
    handleDialog = (bool, params = {}, operate) => {
        this.maintitle = params.name;
        this.visible = bool;
        if (bool) {
            let type = { viewtype: "view" }
            if (operate == "0") {
                this.rightMenu = [];
                type.viewtype = "view";
            }
            this.initListInfo({ ...params, ...type });
            this.paramslist = { ...params };
        }
    }

    //获取盘点计划列表明细
    initListInfo = (params = {}) => {
        this.planlistForm = new WeaForm();
        this.selectid = params.id;
        this.subListData = [];
        API_LIST.getMyPlanCptList(params).then(data => {
            this.basicInfo = data.mainTableInfo;
            this.canEditCpt = data.canEditCpt;
            this.plantableStore = new TableStore();
            this.plantableStore.getDatas(data.subTableInfo.sessionkey);
            this.sessionkey_plan = data.subTableInfo.sessionkey;
            this.loading = false;
            //this.canEditlist = data.mainTableInfo[0].canEdit;
            //根据高级搜索条件初始化form
            this.planlistForm && !this.planlistForm.isFormInit && this.planlistForm.initFormFields(data.mainTableInfo);
        })
    }

    //盘点资产 获取表单
    getInventoryForm = (params = {}) => {
        this.inventoryId = params.inventoryId;
        this.inventoryform = new WeaForm();
        const newParams = { ...params, actionType: "edit", viewScope: "CptInventory" };
        API_LIST.getInventoryForm(newParams).then((data) => {
            this.inventoryformCondition = data.conditioninfo;
            this.inventoryformCheckStr = data.conditioninfo[0].checkstr
            this.inventoryform && this.inventoryform.initFormFields(this.inventoryformCondition);
        });
    }

    //执行盘点
    doInventory = (params = {}) => {
        this.inventoryform.validateForm().then(f =>{
            if(f.isValid){
                const searchParamsAd = this.inventoryform.getFormParams();
                const newParams = { ...params, ...searchParamsAd, id: this.inventoryId };
                API_LIST.doInventory(newParams).then((data) => {
                    if (data == true) {
                        message.success(getLabel('83885','操作成功！'));
                        this.doSearch();
                    } else {
                        message.error(getLabel('385409',"开始盘点失败,请联系管理员!"));
                    }
                    this.handleDialog(true, { id: this.selectid, name: this.maintitle }, 0);
                });
                this.setVisiable(false, 1);
            }else{
                f.showErrors();
            }
        });
    }

    //提交  
    onSubmit = (params = {}) => {
        API_LIST.onSubmit(params).then(data => {
            if (data == true) {
                message.success(getLabel('83885','操作成功！'));
            } else {
                message.error(getLabel('385410',"失败,请联系管理员!"));
            }
            this.doSearch();
        });
    }


    //盘点表生成
    regenTemplate = (params = {}) => {
        const newParams = { ...params, id: this.selectid };
        API_LIST.generateFile(newParams).then((data) => {
            window.location = "/weaver/weaver.file.ExcelOut";
        });
    }

    //导入盘点
    doImport = () => {
        let _this = this;
        if(this.excelfile==''){
            // Modal.warning({
            //     title:  getLabel(15172,'系统提示'),
            //     content: getLabel('384922',"未选择Excel文件"),
            // });
            this.remind.showError();
            return;
        }else{
            let params = {listid: _this.selectid,inventoryexcelfile:this.excelfile||''};
            _this.spinning = true;
            API_LIST.doImportInventory(params).then(data => {
                if(data) {
                    _this.reflesh++;
                    _this.spinning = false;
                    _this.msg = data.msg;
                    this.doSearch();
                }
            })
        }
    }

    clearStatus = () => {
        this.msg = "";
        this.reflesh++;
        this.excelfile="";
        this.filelist=[];
    }

    //校验必填字段
    check_form = (type) => {
        let fields = {}
        let checkstr = ""
        if (type == "doInventory") {
            fields = toJS(this.inventoryform.getFormParams());
            checkstr = toJS(this.inventoryformCheckStr);
        }
        let checkitem = checkstr.split(",");
        let i;
        for (i = 0; i < checkitem.length; i++) {
            let checkid = checkitem[i];
            if (checkid) {
                if (!fields[checkid]) {
                    // Modal.warning({
                    //     title:  getLabel(15172,'系统提示'),
                    //     content: getLabel(30933,'必填信息不完整!'),
                    // });
                    message.error(getLabel(30933,'必填信息不完整!'));
                    return false;
                }
            }
        }
        return true;
    }
}